package org.ant.gui.actionListeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import org.ant.ctrl.CodesManager;
import org.ant.gui.AntFrame;
import org.ant.util.Code;

public class SaveAsActionListener implements ActionListener {

	private AntFrame frame;

	public SaveAsActionListener() {
	}

	public void actionPerformed(ActionEvent arg0) {
		System.out.println("SaveAs Action:");
		frame = AntFrame.getInstance();
		saveAs();
	}

	public int saveAs() {
		int choice = 0;
		try {
			frame = AntFrame.getInstance();
			JFileChooser chooser = new JFileChooser();
			chooser.setFileFilter(new FileFilter() {

				@Override
				public boolean accept(File f) {
					return f.isDirectory()
							|| f.getName().toLowerCase().endsWith(".egg");
				}

				@Override
				public String getDescription() {
					return "Egg file";
				}
			});
			int returnVal = chooser.showSaveDialog(frame);
			File file = null;
			Code code = CodesManager.getInstane().getCurrentCode();
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				file = chooser.getSelectedFile();
			} else {
				return JFileChooser.CANCEL_OPTION;
			}
			if (file.getName().lastIndexOf(".") < 0) {
				file = new File(file.getPath());
				code.setPath(file.getPath());
			}
			if (file != null && file.exists()) {
				int confirm = JOptionPane.showConfirmDialog(frame,
						"File already exists, OVERWRITE?", "File Overwrite",
						JOptionPane.YES_NO_OPTION);
				switch (confirm) {
				case JOptionPane.YES_OPTION:
					saveFile(file, frame.getInputTabbedPane().getCurrentPane()
							.getText());
					code.compile();
					choice = JOptionPane.YES_OPTION;
					break;
				case JOptionPane.NO_OPTION:
					choice = JOptionPane.NO_OPTION;
					break;
				}
			} else {
				System.out.println(file.toString());
				
				//if(frame.getInputTabbedPane().getCurrentPane() == null){
				//	System.out.println("OOXX");
				//}
				//System.out.println(frame.getInputTabbedPane() == null);
				saveFile(file, frame.getInputTabbedPane().getCurrentPane()
						.getText());
				code.compile();
			}
			CodesManager.getInstane().deleteCurrentCode();
			CodesManager.getInstane().openCode(file.getPath());
			//frame.getSideTabbedPane().getClassPane().update();
			//CodesManager.getInstane().broadCasting("OPEN");
			// elementManager.setPath(mapXML.getPath());
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(frame,
					"Error saving the current file", "Error",
					JOptionPane.ERROR_MESSAGE);
			ex.printStackTrace();
		}
		return choice;
	}

	public void saveFile(File file, String doc) {
		try {
			FileWriter fw = new FileWriter(file);
			fw.write(doc);
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
